package com.boot.consumer.config;

import com.boot.consumer.service.impl.OrderServiceImpl;
import com.ego.inter.service.OrderService;
import com.ego.inter.service.UserService;
import org.apache.catalina.User;
import org.apache.dubbo.config.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 服务消费者
 * @date 2020/10/13 8:35
 */
@Configuration
public class DubboConfig {

    /**
     * 声明服务名称
     * @return config
     */
    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig config = new ApplicationConfig("boot-consumer");
        return config;
    }

    /**
     * 声明注册中心配置
     * @return config
     */
    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig config = new RegistryConfig("zookeeper://localhost:2181");
        return config;
    }


    /**
     * 引用UserServiceImpl服务
     * @param orderServiceImpl
     * @return
     */
    @Bean
    public ReferenceConfig<UserService> referenceConfig(OrderServiceImpl orderServiceImpl,
                                                        ApplicationConfig applicationConfig,
                                                        RegistryConfig registryConfig){

        ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(UserService.class);
        referenceConfig.setId("userService");
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
//        给orderServiceImpl注入数据
        orderServiceImpl.setUserService(referenceConfig.get());
        return referenceConfig;
    }
}
